using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using NPC.Enemy;

namespace FrameWork.HTN
{
    public partial

    class HTNPlanBuilder
    {
        private HTNPlanner planner;
        private HTNPlanRunner runner;
        private readonly Stack<IBaseTask> taskStack;

        public HTNPlanBuilder()
        {
            taskStack = new Stack<IBaseTask>();
        }

        private void AddTask(IBaseTask task)
        {
            if (planner != null) // 当前计划器不为空
            {
                // 将新任务作为构造栈顶元素的子任务
                taskStack.Peek().AddNextTask(task);
            }
            else // 如果计划器为空，意味着新任务是根任务，进行初始化
            {
                planner = new HTNPlanner(task as CompoundTask);
                runner = new HTNPlanRunner(planner);
            }

            // 如果新任务是原子任务，就不需要进栈了，因为原子任务不会有子任务
            if (task is not PrimitiveTask)
            {
                taskStack.Push(task);
            }
        }

        // 剩下的代码都很简单，我相信能直接看得懂
        public void RunPlan()
        {
            runner.RunPlan();
        }

        public HTNPlanBuilder Back()
        {
            taskStack.Pop();
            return this;
        }

        public HTNPlanner End()
        {
            taskStack.Clear();
            return planner;
        }

        /// <summary>
        /// 复合任务
        /// </summary>
        /// <returns></returns>
        public HTNPlanBuilder CompoundTask()
        {
            var task = new CompoundTask();
            AddTask(task);
            return this;
        }

        /// <summary>
        /// 方法
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public HTNPlanBuilder Method(Func<bool> condition)
        {
            var task = new Method(condition);
            AddTask(task);
            return this;
        }

        public HTNPlanBuilder Boss_Patrol(BossAgent agent, Vector2 leftPatrol, Vector2 rightPatrol)
        {
            var task = new BossPatrol(agent, leftPatrol, rightPatrol);
            AddTask(task);
            return this;
        }

        // BOSS追击任务
        public HTNPlanBuilder Boss_Chase(BossAgent agent)
        {
            var task = new BossChase(agent);
            AddTask(task);
            return this;
        }

        // BOSS发射火球任务
        public HTNPlanBuilder Boss_Fireball(BossAgent agent)
        {
            var task = new BossFireball(agent);
            AddTask(task);
            return this;
        }

        // BOSS召唤史莱姆任务
        public HTNPlanBuilder Boss_SummonSlime(BossAgent agent)
        {
            var task = new BossSummonSlime(agent);
            AddTask(task);
            return this;
        }

        // BOSS暴怒状态任务
        public HTNPlanBuilder Boss_BerserkState(BossAgent agent)
        {
            var task = new BossBerserkState(agent);
            AddTask(task);
            return this;
        }
    }

}